package com.heei.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @program: spring-cloud-learning
 * @description: Eureka-consumer 消费者接口
 * @author: HuPing
 * @create: 2020-10-22 10:36
 **/
@RestController
public class DcController {

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/dc")
    public String dc() {
        return "Services: " + discoveryClient.getServices();
    }

    @GetMapping("/consumer")
    public String consumer() {

//        ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");
//        String clientDcUrl = "http://" + serviceInstance.getHost()+":"+serviceInstance.getPort()+"/dc";
//        String clientDcUrl = "http://127.0.0.1:20010/dc";

        return restTemplate.getForObject("http://eureka-client/dc", String.class);
    }
}
